package com.boot2.core.utils.ip;

import com.boot2.core.utils.StringUtils;

public class IPLocation {

    String[] chineseMotherLandProvince = {"北京", "天津", "上海", "重庆", "河北", "山西", "辽宁", "吉林", "黑龙江", "江苏",
            "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "海南",
            "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾", "内蒙古", "广西壮族", "西藏", "宁夏"
            , "新疆" };

    /**
     * 国家
     */
    private String country;
    /**
     * 区域 - 省份 + 城市
     */
    private String area;

    public IPLocation() {
        country = area = "";
    }

    public synchronized IPLocation getCopy() {
        IPLocation ret = new IPLocation();
        ret.country = country;
        ret.area = area;
        return ret;
    }

    public String getCountry() {
        for (String province : chineseMotherLandProvince) {
            if (country.contains(province)) {
                return "中国大陆";
            }
        }
        return country;
    }

    public String getLocationInfo() {
        return country;
    }

    public String getProvince() {
        if (StringUtils.isEmpty(country)) {
            return "";
        }
        int provinceIndex = country.indexOf("省");
        if (provinceIndex != -1) {
            String province = country.substring(0, provinceIndex);
            return province;
        }
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        String city = "";
        if (country != null) {
            String[] array = country.split("省");
            if (array != null && array.length > 1) {
                city = array[1];
            } else {
                city = country;
            }
            if (city.length() > 3) {
                city.replace("内蒙古", "");
            }
        }
        return city;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        //如果为局域网，纯真IP地址库的地区会显示CZ88.NET,这里把它去掉
        if (area.trim().equals("CZ88.NET")) {
            this.area = "本机或本网络";
        } else {
            this.area = area;
        }
    }
}